import { article as ArticleApi } from '../../../../../config/request.js';
import { Loading } from 'element-ui';
import resources from '../../../../../util/resources.js';

module.exports = {
    name: 'editor',
    data() {
        return {
            content: '11',
            updateFlag: false,
            articleForm: {
                title: '',
                html: '',
                remark: '',
                author: '',
                type: 1
            },
            aid: '',
            editor: {},
        }
    },
    methods: {
        updateData(data) {
            this.articleForm.html = data;
        },
        /** 提交表单 */
        submitForm(formName) {
            var article = {};
            Object.assign(article, this[formName]);
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //添加article
                    if (this.updateFlag) {
                        article.id = this.aid;
                        this._updateArticle(article);
                    } else {
                        article.status = 0;
                        this._createArticle(article);
                    }

                } else {
                    return false;
                }
            });
        },
        _updateArticle(article) {
            var loading = Loading.service({ fullscreen: false, target: editor, text: '文章修改中...' });
            ArticleApi.updateArticle.call(this, article,
                //添加成功
                (data) => {
                    loading.close();
                    this.$message.success('修改文章成功 ！！');
                    this.cancel();
                },
                //添加失败
                (msg) => {
                    loading.close();
                    this.$message.error('添加文章失败 ！！');
                })
        },
        _createArticle(article) {
            var loading = Loading.service({ fullscreen: false, target: editor, text: '文章创建中...' });
            ArticleApi.createArticle.call(this, article,
                //添加成功
                (data) => {
                    loading.close();
                    this.$message.success('添加文章成功 ！！');
                    this.cancel();
                },
                //添加失败
                (msg) => {
                    loading.close();
                    this.$message.error('添加文章失败 ！！');
                })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        cancel() {
            this.articleForm = {
                title: '',
                html: '',
                remark: '',
                author: '',
                type: 1
            }
            this.$refs['articleForm'].resetFields();
            this.$router.push('/business/htmlconfig/article');
        },
        articleTypes() {
            return resources.aType;
        }
    },
    mounted: function() {
        this.editor = document.getElementById('editor');
        this.updateFlag = false;
        if (this.$route.query.updateFlag) {
            this.updateFlag = this.$route.query.updateFlag == 'true';
        }
        if (this.updateFlag && this.$route.query.aid) {
            var loading = Loading.service({ fullscreen: false, target: editor, text: '文章数据加载中...' });
            this.aid = this.$route.query.aid;
            ArticleApi.queryArticleById.call(this, { id: this.aid }, data => {
                var article = data['article'];
                loading.close();
                this.articleForm = {
                    title: article.title,
                    remark: article.remark,
                    html: article.html,
                    author: article.author,
                    type: article.type
                }
            })
        }
    },
    created: function() {}
}